import cacheKeys from '@/common/app/cacheKeys.js'
import { localStorage } from '@/common/app/storage.js'

export default {
  state: {
    // 缓存用户是否已读服务协议和隐私政策，true：已签署，false：未签署
    signedAgreement: localStorage.getBoolean(cacheKeys.CACHE_SIGNED_AGREEMENT) || false,
    // 缓存全局系统当前时间，在App页面启动定时器，每一秒刷新系统时间
    currentTime: {},
    // 缓存系统全量枚举信息
    enumList: localStorage.getObject(cacheKeys.CACHE_ENUM_LIST) || {}
  },
  getters: {
    signedAgreement(state) {
      return state.signedAgreement
    },
    currentTime(state) {
      return state.currentTime
    },
    enumList(state) {
      return state.enumList
    }
  },
  mutations: {
    [cacheKeys.CACHE_SIGNED_AGREEMENT](state, data) {
      state.signedAgreement = data;
      localStorage.setBoolean(cacheKeys.CACHE_SIGNED_AGREEMENT, data)
      if (data === undefined || data === null || data === '') {
        localStorage.removeItem(cacheKeys.CACHE_SIGNED_AGREEMENT)
      }
    },
    currentTime(state, data) {
      state.currentTime = data;
    },
    [cacheKeys.CACHE_ENUM_LIST](state, data) {
      state.enumList = data;
      localStorage.setObject(cacheKeys.CACHE_ENUM_LIST, data)
      if (data === undefined || data === null || data === '') {
        localStorage.removeItem(cacheKeys.CACHE_ENUM_LIST)
      }
    }
  }
}
